package mysql

import (
	"novel-hub/novelrpc/novelrpc"

	"gorm.io/gorm"
)

type Content struct {
	gorm.Model
	UserId  int64  `gorm:"type:int(11);not null;comment:'用户 ID'"`
	NovelId int64  `gorm:"type:int(11);not null;comment:'小说 ID'"`
	Content string `gorm:"type:varchar(500);not null;comment:'评价内容'"`
}

func (c *Content) ContentAdd(db *gorm.DB) error {
	return db.Debug().Create(c).Error
}

func (c *Content) ContentList(db *gorm.DB, title string, lists []*novelrpc.ContentList) ([]*novelrpc.ContentList, error) {
	/*
		SELECT
		contents.id as content_id,users.nickname,contents.content
		FROM
		contents
		LEFT JOIN users ON users.id=contents.user_id
		LEFT JOIN novels ON novels.id=contents.novel_id
	*/
	err = db.Debug().Model(c).
		Select("contents.id as content_id,users.nickname,contents.content").
		Joins("LEFT JOIN users ON users.id=contents.user_id").
		Joins("LEFT JOIN novels ON novels.id=contents.novel_id").
		Where("novels.title like ?", "%"+title+"%").
		Order("contents.created_at desc").
		Find(&lists).Error
	return lists, err
}
